/* eslint-disable jsdoc/no-restricted-syntax */
// 实现一个 Promise.race 方法

/**
 * @description 实现一个 Promise.race 方法
 * @template {(PromiseLike<any> | unknown)[]} T
 * @param {T} promises
 * @returns {Promise<any>}
 */
export function race(promises) {
  if (promises.length === 0) {
    return Promise.resolve();
  }
  return new Promise((resolve, reject) => {
    for (const promise of promises) {
      if (promise instanceof Promise) {
        Promise.resolve(promise).then(resolve, reject);
      } else {
        try {
          resolve(promise);
        } catch (error) {
          reject(error);
        }
      }
    }
  });
}
